package cn.e3.cart.service.impl;




import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import cn.e3.cart.service.AddCartToRedis;
import cn.e3.jedis.JedisClient;
import cn.e3.mapper.TbItemMapper;
import cn.e3.pojo.TbItem;
import cn.e3.utils.E3Result;
import cn.e3.utils.JsonUtils;
@Service
public class AddCartToRedisImpl implements AddCartToRedis{
	@Autowired
	private JedisClient jedisClient;
	@Value("$(REDIS_CART_PRE)")
	private String REDIS_CART_PRE;
	@Autowired
	private TbItemMapper itemMapper;
	@Override
	public E3Result addCart(Long userId, Long itemId, Integer num) {
		//以userId为key,itemId为filed,商品为value存储
		Boolean b = jedisClient.hexists(REDIS_CART_PRE+userId+"", itemId+"");

		if(b){
			//此时存在，设置数量
			String json = jedisClient.hget(REDIS_CART_PRE+userId+"", itemId+"");
			//json转换为对象
			TbItem tbItem = JsonUtils.jsonToPojo(json, TbItem.class);
			tbItem.setNum(tbItem.getNum()+num);
			//将该对象写回json串
			String toJson = JsonUtils.objectToJson(tbItem);
			//写回redis
			jedisClient.hset(REDIS_CART_PRE+userId+"", itemId+"", toJson);
			return E3Result.ok();
		}
		//此时商品不存在，重新添加商品
		//查询商品
		TbItem tbItem = itemMapper.selectByPrimaryKey(itemId);
		//设置其数量
		tbItem.setNum(num);
		//设置图片，取一张
		String image = tbItem.getImage();
		if(StringUtils.isNotBlank(image)){
			String[] split = image.split(",");
			tbItem.setImage(split[0]);
		}
		//将商品加入list中
		//list.add(tbItem);
		//list转为json串
		String jsonList = JsonUtils.objectToJson(tbItem);
		//保存到hset中
		jedisClient.hset(REDIS_CART_PRE+userId+"", itemId+"", jsonList);
		return E3Result.ok();
	}
	/*
	 * 合并购物车
	 */
	public E3Result merge(Long userId, List<TbItem> list) {
		//判断redis中是否存在该商品，有则添加数量，没有，则重新添加
		for (TbItem tbItem : list) {
			addCart(userId, tbItem.getId(), tbItem.getNum());
		}
		return E3Result.ok();
	}
	//修改redis的购物车的商品单个数
	public E3Result changeNum(Long userId, Long itemId, int num) {
		//查询redis,获取商品json串
		String json = jedisClient.hget(REDIS_CART_PRE+userId+"", itemId+"");
		//转化为对象
		TbItem tbItem = JsonUtils.jsonToPojo(json, TbItem.class);
		//设置其数量
		tbItem.setNum(num);
		//转为json,写入redis
		jedisClient.hset(REDIS_CART_PRE+userId+"", itemId+"", JsonUtils.objectToJson(tbItem));
		return E3Result.ok();
	}
	//删除redis中购物项
	public String delete(Long userId, Long itemId) {
		//删除该购物项
		jedisClient.hdel(REDIS_CART_PRE+userId+"", itemId+"");
		return "redirect:/cart/cart.html";
	}
	//显示购物项
	public List<TbItem> showCart(Long userId) {
		//得到hset中的值
		List<String> list = jedisClient.hvals(REDIS_CART_PRE+userId+"");
		List<TbItem> list1=new ArrayList<TbItem>();
		//遍历集合，将其转换为tbitem
		for(String s :list){
			list1.add(JsonUtils.jsonToPojo(s, TbItem.class));
		}
		return list1;
	}
	@Override
	public E3Result cleanCart(Long userId) {
		//删除当前用户的购物车
		jedisClient.del(REDIS_CART_PRE+userId+"");
		return E3Result.ok();
	}

}
